--朝谒观途
local chaojieguantu = fk.CreateSkill {
    name = "mcdh_chaojieguantu",
    tags = {Skill.Lord}
}

chaojieguantu:addEffect(fk.PindianFinished,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(chaojieguantu.name) and (data.from == player or table.contains(data.tos,player))
    end,
    on_trigger=function (self, event, target, player, data)
        local room = player.room
        local targets = {}
        table.insertIfNeed(targets,data.from)
        table.insertTableIfNeed(targets,data.tos)
        table.removeOne(targets,player)
        room:sortByAction(targets)
        for _, p in pairs(targets) do
            if not player:hasSkill(chaojieguantu.name) or player.dead then
                break
            end
            if not p.dead then
                 self:doCost(event, p, player, data)
            end
        end
    end,
    on_cost=function (self, event, target, player, data)
        local room = player.room
        local logic = room.logic
        local Pindian_event = logic:getCurrentEvent():findParent(GameEvent.Pindian, true)
        if Pindian_event == nil then return false end
        local events =logic:getEventsOfScope(GameEvent.Pindian, 1, function (e)
        local last_Pian = e.data
        if (data.from == target or table.contains(data.tos ,target)) and(data.from == player or table.contains(data.tos ,player)) then
            return true
        end
            return false
        end, Player.HistoryTurn)
        if #events ~= 0 and events[1].id == Pindian_event.id then
             return room:askToSkillInvoke(target,{prompt = "#chaojieguantu:"..player.id,skill_name=chaojieguantu.name})
        end
    end,
    on_use=function (self, event, target, player, data)
        local room = player.room
        local  all_cards = room:getNCards(2, 'top')
        local cards = table.simpleClone(all_cards)
        local ids = room:askForCard(player, 1, 999, false, self.name, false, tostring(Exppattern{ id = cards }),
                "#chaojieguantu-give:"..target.id, cards)
        for _, id in ipairs(ids) do
            table.removeOne(cards, id)
            room:setCardMark(Fk:getCardById(id), "@DistributionTo", Fk:translate(target.general))
        end
        if #cards == 0 then
            for _, id in ipairs(ids) do
                room:setCardMark(Fk:getCardById(id), "@DistributionTo", 0)
            end
            room:moveCardTo(ids, Card.PlayerHand, target, fk.ReasonGive, self.name, nil, true, player.id)
        else
            local list = room:askToYiji(player,{cards=cards,min_num=#cards,max_num=#cards,skill_name=chaojieguantu.name})
            for _, id in ipairs(ids) do
                table.insert(list[target.id], id)
            end
            for _, id in ipairs(all_cards) do
                room:setCardMark(Fk:getCardById(id), "@DistributionTo", 0)
            end
            room:doYiji(list, player.id, self.name)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_chaojieguantu"] ="朝谒观途",
    [":mcdh_chaojieguantu"] = "主公技，其他角色每回合首次与你拼点结束后，其可以令你观看并分配牌堆顶两张牌。（须包含其）",
    ["#chaojieguantu"] = "朝谒观途:令 %src 观看并分配牌堆顶两张牌。（你至少获得一张）",
    ["#chaojieguantu-give"] = "朝谒观途: 分配给 %src 至少一张牌"
}


return chaojieguantu